package com.atguigu.eduservice.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EduCourseMapper eduCourseMapper;

    /*条件分页查询老师信息*/
    @Override
    public R getTeacherCondition(long current, long limit, TeacherQuery teacherQuery) {
        /*1.获取查询条件*/
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        /*2.构造查询条件*/
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level", level);
        }
        /*按照gmt_create降序排序*/
        wrapper.orderByDesc("gmt_create");
        /*3.分页查询结果*/
        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> pageResult = page(page, wrapper);
        long total = pageResult.getTotal();
        List<EduTeacher> records = pageResult.getRecords();
        if(records==null){
            return R.error();
        }
        /*4.构造返回值*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    /*发布新课程里面的查询所有讲师的接口*/
    @Override
    public R findAll() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        List<EduTeacher> eduTeachers = baseMapper.selectList(wrapper);
        /*这里的items是 response.data.items 里面的items*/
        return R.ok().data("items", eduTeachers);
    }

    /*查询4个老师集合*/
    @Override
    public List<EduTeacher> selectTeacher() {
        List<EduTeacher> teacherList=new ArrayList<>();
        /*1.先去redis查询*/
        String redisKey="teacherList:";
        String redisValue = stringRedisTemplate.opsForValue().get(redisKey);
        if(StringUtils.isEmpty(redisValue)){
            /*查询数据库*/
            QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
            wrapper.last("limit 4");
            teacherList = baseMapper.selectList(wrapper);
            if(!StringUtils.isEmpty(teacherList)){
                stringRedisTemplate.opsForValue().set(redisKey,JSONObject.toJSONString(teacherList));
            }
            return teacherList;
        }else {
            List<EduTeacher> eduTeachers = JSONArray.parseArray(redisValue, EduTeacher.class);
            return eduTeachers;
        }
    }

    /*前台分页查询所有老师信息*/
    @Override
    public R frontTeacherPage(long current, long limit) {
        Page<EduTeacher> iPage = (Page<EduTeacher>) baseMapper.selectPage(new Page<EduTeacher>(current, limit), null);
        /*获取信息并封装信息*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("items",iPage.getRecords());
        map.put("current",iPage.getCurrent());
        map.put("pages",iPage.getPages());
        map.put("size",iPage.getSize());
        map.put("total",iPage.getTotal());
        map.put("hasNext",iPage.hasNext());
        map.put("hasPrevious",iPage.hasPrevious());
        return R.ok().data(map);
    }

    /*前台根据老师的id查询老师的详细信息+老师的主讲课程*/
    @Override
    public R selectTeacherById(long id) {
        /*查询老师的详细信息*/
        EduTeacher eduTeacher = baseMapper.selectById(id);
        /*查询老师的主讲课程*/
        List<EduCourse> eduCourses = eduCourseMapper.selectList(new QueryWrapper<EduCourse>().eq("teacher_id", String.valueOf(id)).orderByDesc("gmt_modified"));
        return R.ok().data("teacher", eduTeacher).data("courseList", eduCourses);
    }


}
